<template>
	<view>
		<uni-section title="搜索" type="line">
			<uni-search-bar @confirm="search" v-model="params.name"	@cancel="cancel" @clear="cancel">
			</uni-search-bar>
		</uni-section>
		<uni-section title="文档列表" type="line">
			<uni-list>
				<uni-list-item v-for="(item, i) in list" :key="i" clickable
					:title="item.name" show-arrow @click="showDetail(item)"
					:rightText="item.cateName">			
				</uni-list-item>
			</uni-list>
		</uni-section>
		<uni-load-more :status="moreStatus" @clickLoadMore="getList"/>
	</view>
</template>

<script>
	import {listDocument} from '@/api/base.js'
	import config from '@/config'
	const baseUrl = config.baseUrl
	export default {
		data() {
			return {
				list:[],
				moreStatus:'noMore',
				params:{
					name:null,
					pageSize: 10,
					pageNum: 1,
					orderByColumn: 't.id',
					isAsc: 'DESC'
				}
			}
		},
		onLoad() {
			this.getList();
		},
		methods: {
			getList() {
				this.moreStatus = 'loading';
				listDocument(this.params).then(response=>{
					if(response.rows == null || response.rows.length == 0){
						this.moreStatus = 'noMore';
					}
					
					if(this.params.pageNum == 1){
						this.list = response.rows;
					}else{
						this.list = this.list.concat(response.rows);
					}
					
					if(response.rows.length < this.params.pageSize){
						this.moreStatus = 'noMore';
					}else{						
						this.moreStatus = 'more';
						this.params.pageNum += 1;
					}
				})
			},
			search(res) {
				this.params.pageNum = 1;
				this.list = [];
				this.getList();
			},
			cancel(res) {
				this.params.name = null;
				this.search();
			},
			showDetail(item) {		
				let that = this;
				uni.showActionSheet({
					itemList: ['打开PDF', '下载PDF', '下载原始文件'],
					success: function (res) {
						if(res.tapIndex == 0){
							if(!item.pdf){
								that.$modal.msgError("未上传PDF文件");
							}else{
								that.openPdf(baseUrl + item.pdf);
							}
						}else if(res.tapIndex == 1){
							if(!item.pdf){
								that.$modal.msgError("未上传PDF文件");
							}else{
								that.downLoad(baseUrl + item.pdf);
							}
						}else if(res.tapIndex == 2){
							if(!item.file){
								that.$modal.msgError("未上传原始文件");
							}else{
								that.downLoad(baseUrl + item.file);
							}
						}
					},
					fail: function (res) {
						console.log(res.errMsg);
					}
				});
			},
			downLoad(path) {
				uni.downloadFile({
				  url: path,
				  success: function (res) {
					  uni.saveFile({
						tempFilePath: res.tempFilePath,
						success: function (res) {
							this.msgSuccess('文件已成功保存到:'+res.savedFilePath);
						}
					  });
				  }
				});
			},
			openPdf(path) {
				uni.downloadFile({
				  url: path,
				  success: function (res) {
				    var filePath = res.tempFilePath;
				    uni.openDocument({
				      filePath: filePath,
				      showMenu: true
				    });
				  }
				});
			}
		}
	}
</script>

<style>
	.uni-common-mt {
		margin-top: 10px;
	}

	.uni-padding-wrap {
		// width: 750rpx;
		padding: 0px 10px;
	}
</style>